﻿#region namespaces

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Web;

using SLIITPro.Core;
using SLIITPro.Data;
using SLIITPro.Data.Entity;
using SLIITPro.Data.Extension;

#endregion

namespace SLIITPro
{
    public partial class Services : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            btnSearch.Click += new EventHandler(btnSearch_Click);
            txtSearch.TextChanged += new EventHandler(txtSearch_TextChanged);
            if (Request.QueryString.ToString() == "set")
            {
                Session.Remove("country");
                Session.Remove("street");
                Response.Redirect(Request.UrlReferrer.ToString());

            }
            if (!IsPostBack)
            {
                Utility.BindCountries(ddlContry);
                SetPanels();
                if (!string.IsNullOrEmpty(Request.QueryString["q"]) || !string.IsNullOrEmpty(Request.QueryString["Type"]))
                {
                    Page.Title = string.Format("Search result for '{0}'", Server.HtmlEncode(Request.QueryString["q"]));
                    headsearch.InnerHtml = string.Format("Search result for : ' {0} '", Server.HtmlEncode(Request.QueryString["q"]));
                    var searchTerm = Utility.CleanStrContent(Request.QueryString["q"]);
                    var searchType = Utility.CleanStrContent(Request.QueryString["Type"]);
                    SearchContent(searchTerm, searchType);
                }
                else
                {
                    Page.Title = "Search";
                    headsearch.InnerHtml = "Search";
                }
            }


            lblLocation.Text = GetMessage;
            lblLocation.NavigateUrl = CoreSettings.RelativeWebRoot + "Services.aspx?set";
        }

        private Search search;
        private void SearchContent(string searchTerm, string searchType)
        {
            if (string.IsNullOrEmpty(searchTerm)) return;
            search = new Search();

            List<BusinessItem> list = search.GetSearchResults(searchTerm.Trim(), searchType.Trim(), Session["city"].ToString());
            if (list.Count > 0)
            {
                RepeaterSearchResult.DataSource = list;
                RepeaterSearchResult.DataBind();
            }
            else
            {
                lblMessage.Text = "No Data Found";
            }
        }

        void txtSearch_TextChanged(object sender, EventArgs e)
        {
            Response.Redirect(string.Format("Services.aspx?q={0}&type={1}", Server.UrlPathEncode(txtSearch.Text), Server.UrlPathEncode(ddlSearchTerm.SelectedValue)));
        }

        void btnSearch_Click(object sender, EventArgs e)
        {
            Response.Redirect(String.Format("Services.aspx?q={0}&type={1}", Server.UrlPathEncode(txtSearch.Text), Server.UrlPathEncode(ddlSearchTerm.SelectedValue)));
        }

        public string GetMessage
        {
            get
            {
                return Session["country"] == null || Session["city"] == null || Session["street"] == null
                           ? "Set your location"
                           : string.Format("You current location is {0},{1} - <b>{2}</b>", Session["city"],
                                           Session["street"], Utility.GetCountry(Session["country"].ToString()));
            }
        }

        protected void btnSetLocation_Click(object sender, EventArgs e)
        {
            Session.Add("country", ddlContry.SelectedValue);
            Session.Add("city", txtCity.Text);
            Session.Add("street", txtStreet.Text);
            lblLocation.Text = GetMessage;
            MultiViewSearch.ActiveViewIndex = 1;
        }

        private void SetPanels()
        {
            if (Session["country"] == null || Session["city"] == null || Session["street"] == null)
            {
                MultiViewSearch.ActiveViewIndex = 0;
            }

            else
            {
                MultiViewSearch.ActiveViewIndex = 1;
            }
        }

    }
}
